﻿/*****************************************************
Module Name:  Query form Javascript Code
Created By:   Justin Jiang
Created Date: 2008-12-18
Description:
    1. Add Popup function used for Popup a 
fixed window
*****************************************************/

function KeyValuePair() { }
KeyValuePair.prototype.Key = "";
KeyValuePair.prototype.Value = "";

// Get control's left position
function getLeft(el)
{   
     return el == null ? 0 : (el.offsetLeft + getLeft(el.offsetParent));   
}

// Get controls's top position
function getTop(el)
{   
     return el == null ? 0 : (el.offsetTop + getTop(el.offsetParent));   
}

// Define QueryForm class
function QueryForm(){}

// Self defined singleton constructor
QueryForm.instance = null;
QueryForm.getInstance = function()
{
    if (QueryForm.instance == null)
    {
        QueryForm.instance = new QueryForm();
        QueryForm.instance._currentDiv = null;
        QueryForm.instance._targetControlId = null;
        QueryForm.instance._origionalValues = new Array();
    }
    return QueryForm.instance;
}

// On Show Div
QueryForm.prototype.ShowDiv = function(divId, hostControlId)
{
    // Keep only one Div visible
    if (QueryForm.getInstance()._currentDiv != null)
    {
        QueryForm.getInstance()._currentDiv.style.display = "none";
        if (QueryForm.getInstance()._targetControlId != null)
        {
            var hostControl = document.getElementById(hostControlId);
            hostControl.className = "hideQuery";
            SetAllSelectsValue(QueryForm.getInstance()._origionalValues);
        }
        if (QueryForm.getInstance()._currentDiv.id == divId)
        {
            QueryForm.getInstance()._currentDiv = null;
            return;
        }
    }
    var div = document.getElementById(divId);
    QueryForm.getInstance()._currentDiv = div;
    QueryForm.getInstance()._targetControlId = hostControlId;
    
    // Override global onmousedown event
    document.body.onmousedown = function()
    {
        if (QueryForm.getInstance()._currentDiv == null)
        {
            return;
        }
        // If SourceElement is not current div, then close current div
        var pElement = event.srcElement;
        
        // Ignore target control click
        if (pElement.id == QueryForm.getInstance()._targetControlId) 
        {
            return;
        }
        
        // Ignore inner current div control
        var isFind = false;
        while (pElement != null)
        {
            if (pElement.id == QueryForm.getInstance()._currentDiv.id)
            {
                isFind = true;
                break;
            }
            pElement = pElement.parentElement;
        }
        
        // If the control is outside current div control
        // hide the div
        if (!isFind)
        {
            QueryForm.getInstance()._currentDiv.style.display = "none";
            QueryForm.getInstance()._currentDiv = null;
            hostControl.className = "hideQuery";
            SetAllSelectsValue(QueryForm.getInstance()._origionalValues);
        }
    }
    var hostControl = document.getElementById(hostControlId);
    if (div.style.display != "block")
    {
        div.style.display = "block";
        hostControl.className = "showQuery";
        adjustPos(div, hostControl);
        QueryForm.getInstance()._origionalValues = GetAllSelectsValue(div);
    }
}

function adjustPos(div, hostControl)
{
    // Calculate top
    div.style.top = getTop(hostControl) + hostControl.offsetHeight + 7 + "px";
    
    // Calculate left
    var hostLeft = getLeft(hostControl)
    var divWidth = div.offsetWidth;
    var bestOffset = 150;
    if (divWidth < bestOffset)
    {
        div.style.left = hostLeft - 10 + "px";
        return;
    }
    if (hostLeft + bestOffset > document.body.offsetWidth)
    {
        div.style.left = document.body.offsetWidth - divWidth - 10 + "px";
    }
    else
    {
        div.style.left = hostLeft + bestOffset - divWidth + "px";
    }
}
        
// Show or Hide Query Form
function ShowQueryForm(id)
{
    QueryForm.getInstance().ShowDiv(id, event.srcElement.id);
    return false;
}

// Add objects to container
function AddContainserObjs(ids, containerId)
{
    var idArray = ids.split(",");
    var container = document.getElementById(containerId);
    if (container == undefined)
    {
        return;
    }
    
    var i = 0;
    for(i = idArray.length - 1; i >= 0; i--)
    {
        var element = document.getElementById(idArray[i]);
        if (element == undefined)
        {
            continue;
        }
        
        element.parentNode.removeChild(element);
        container.insertBefore(element, container.children[0]);
    }
}

// Get all select selected option value
function GetAllSelectsValue(container)
{
    var selections = new Array();
    var selects = container.getElementsByTagName("SELECT");
    var i = 0;
    for(i = 0; i < selects.length; i++)
    {
        var selection = new KeyValuePair();
        selection.Key = selects[i].id;
        selection.Value = selects[i].selectedIndex;
        selections.push(selection);
    }
    return selections;
}

// Set selected of elements in the array
function SetAllSelectsValue(selections)
{
    var i = 0;
    for(i = 0; i < selections.length; i++)
    {
        var element = document.getElementById(selections[i].Key);
        if (element == undefined)
        {
            continue;
        }
        
        element.selectedIndex = selections[i].Value;
    }
}